/**
 * 床位管理初始化
 */
var CdcBed = {
    id: "CdcBedTable",	//表格id
    seItem: null,		//选中的条目
    table: null,
    layerIndex: -1
};

/**
 * 初始化表格的列
 */
CdcBed.initColumn = function () {
    return [
        {field: 'selectItem', radio: true},
        {title: '索引码', field: 'xcode', align: 'center', valign: 'middle', sortable: true},
        {title: '代码', field: 'code', align: 'center', valign: 'middle', sortable: true},
        {title: '描述', field: 'description', align: 'center', valign: 'middle', sortable: true},
        {title: '所属病房', field: 'room.description', align: 'center', valign: 'middle', sortable: true},
        {title: '是否有效', field: 'isActive', align: 'center', valign: 'middle', sortable: true,formatter:function(value,row,index) {
            return value?"是":"否";
        }}
    ];
};

/**
 * 检查是否选中
 */
CdcBed.check = function () {
    var selected = $('#' + this.id).bootstrapTable('getSelections');
    if(selected.length == 0){
        Sys.info("请先选中表格中的某一记录！");
        return false;
    }else{
        CdcBed.seItem = selected[0];
        return true;
    }
};

/**
 * 点击新增床位
 */
CdcBed.add = function () {
    var index = Sys.open('新增床位',Sys.backendPath + '/cdc/cdcbed/to_add',['800px', '280px']);
    this.layerIndex = index;
};

/**
 * 打开查看床位详情
 */
CdcBed.edit = function (id) {
	var flag = false;
	if(id){
		flag = true;
	}else if (this.check()) {
        id = this.seItem.id;
        flag = true;
    }
	if (flag) {
    	var index = Sys.open('床位详情',Sys.backendPath + '/cdc/cdcbed/to_update/' + id,['800px', '280px']);
        this.layerIndex = index;
    }
};

/**
 * 删除床位
 */
CdcBed.del = function (id,name) {
	var flag = false;
	if(id){
		flag = true;
	}else if (this.check()) {
        id = this.seItem.id;
        name = this.seItem.name
        flag = true;
    }
	if (flag) {
        var operation = function(){
	        var ajax = new $ax(Sys.backendPath + "/cdc/cdcbed/delete", function (data) {
	            Sys.success("删除成功!");
	            CdcBed.table.refresh();
	        }, function (data) {
	            Sys.error("删除失败!" + data.responseJSON.message + "!");
	        });
	        ajax.set("cdcBedId",id);
	        ajax.start();
	    };
	    Sys.confirm("是否删除床位?",operation);
    }
};

/**
 * 查询床位列表
 */
CdcBed.search = function () {
    var queryData = {};
    queryData['code'] = $("#code").val();
    queryData['description'] = $("#description").val();
    queryData['roomId'] = $("#roomId").val();
    queryData['dataId'] = $("#dataId").val();
    queryData['isActive'] = $("#isActive").val();
    CdcBed.table.refresh({query: queryData});
};

$(function () {
    CdcBed.sysDataId=window.parent.baseData.sysDataId;

    var defaultColunms = CdcBed.initColumn();
    var table = new BSTable(CdcBed.id, Sys.backendPath + "/cdc/cdcbed/list", defaultColunms);
    table.setPaginationType("server");
    table.setPageSize(10);
    table.setQueryParams({ "dataId": CdcBed.sysDataId });
    CdcBed.table = table.init();
});
